package com.yuxiao.springboot.upload.controller;


import com.yuxiao.springboot.upload.entity.FileName;
import com.yuxiao.springboot.upload.entity.ProgressEntity;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传及进度测试
 */
@Controller
public class UploadController {


    /**
     * 显示文件上传页
     * @return
     */
    @RequestMapping("uploadPage")
    public String showUploadPage(){
        return "uploadPage";
    }

    /*
     * 文件上传
     * <p>Title: upload</p>
     * <p>Description: </p>
     * @param file
     * @return
     */
    @PostMapping("upload")
    @ResponseBody
    public Map<String, Object> upload(MultipartFile file){
        Map<String, Object> result = new HashMap<>();
        if (file != null && !file.isEmpty()){
            try {
                file.transferTo(new File("/Users/zhangyuting/Desktop/"+file.getOriginalFilename()));
                FileName.path="/Users/zhangyuting/Desktop/"+file.getOriginalFilename();
                result.put("code", 200);
                result.put("msg", "success");
            } catch (IOException e) {
                result.put("code", -1);
                result.put("msg", "文件上传出错，请重新上传！");
                e.printStackTrace();
            }
        } else {
            result.put("code", -1);
            result.put("msg", "未获取到有效的文件信息，请重新上传!");
        }
        return result;
    }


    /**
     * 获取文件上传进度
     * @param request
     * @return
     */
    @RequestMapping("getUploadProgress")
    @ResponseBody
    public ProgressEntity getUploadProgress(HttpServletRequest request){
        return (ProgressEntity) request.getSession().getAttribute("uploadStatus");
    }

    /*
     * 文件上传
     * <p>Title: upload</p>
     * <p>Description: </p>
     * @param file
     * @return
     */
    @PostMapping("calc")
    @ResponseBody
    public Map<String, Object> calc(String type){
        Map<String, Object> result = new HashMap<>();
        if (!FileName.path.equals("")){
            String findStr="";
            if ("1".equals(type)){
                findStr="booting_2";
            }
            if ("2".equals(type)){
                findStr="booting_3";
            }
            long start = System.currentTimeMillis() ;
            Map<String,Integer> map=handle(new File(FileName.path),findStr) ;
            map.forEach((s,i)-> System.out.println(s+" "+i));
            System.out.println("总耗时：" + (System.currentTimeMillis()-start) + "毫秒");
            result.put("value",map);
        } else {
            result.put("code", -1);
            result.put("msg", "未获取到有效的文件信息，请重新上传!");
        }
        return result;
    }

    private Map<String,Integer> handle(File file,String str) {
        Map<String,Integer> result=new HashMap<>();
        LineIterator it=null;
        Integer sum=0;
        try {
            it = FileUtils.lineIterator(file);
            while (it.hasNext()) {
                String line = it.nextLine();
                // do something with line
                if (line.contains(str)){
                    if (!result.containsKey(line.split(",")[1].split(":")[1])){
                        result.put(line.split(",")[1].split(":")[1],1);
                    }else {
                        result.put(line.split(",")[1].split(":")[1],result.get(line.split(",")[1].split(":")[1])+1);
                    }
                    sum++;
                }
            }
            result.put("sum",sum);
        } catch (IOException e){
            e.printStackTrace();
        }finally {
            LineIterator.closeQuietly(it);
        }
        return result;
    }
}
